/**
 * Created by fupeng on 17/3/12.
 */
/**
 * Created by Administrator on 2017/2/9.
 */
var pool = require('./DBConnectPool');

module.exports = {
    getCinema :function (param,callBack){
        pool.getPoolConnection('mtsc', function (err, connection) {
            if (err) {
                console.log('get connection from pool failed in Ciname: ', err);
                return;
            }
            var sql =' select a.*,case  when a.dis < 1000 then concat(a.dis,"m")'
                +' when a.dis > 1000 then concat(round(a.dis/1000,2),"km")'
                +' end as distance '
                +' from (select ci.*,ROUND(6378.138*2*ASIN(SQRT(POW(SIN((?*PI()/180-ci.cinema_lat*PI()/180)/2),2)+COS(?*PI()/180)*COS(ci.cinema_lat*PI()/180)*POW(SIN((?*PI()/180-ci.cinema_long*PI()/180)/2),2)))*1000) as dis'
                +' from ( '+ param.cinematable + ') as ci) as a';
            connection.query(sql, [param.lat,param.lat,param.long],function (queryErr, rows) {

                if (queryErr) {
                    console.log('select * from cinema failed: ', queryErr);
                    callBack({
                        isSuccess: false
                    });
                } else {
                    callBack({
                        isSuccess: true,
                        rows: rows
                    })
                }
                connection.release();
            });

        });
    }
};

